require "Common/define"
require "Logic/Config"
require "Common/functions"

RedDotsBagItemHandler = {};
local this = RedDotsBagItemHandler;
local itemCfg;


function RedDotsBagItemHandler.init()
    CloseEvent(Config.EventType.Fresh_Bag, this.update);
    CloseEvent(Config.EventType.RefreshMergePet,this.update)
    CloseEvent(Config.EventType.TimeBoxUpdate,this.update)
    ListenEvent(Config.EventType.TimeBoxUpdate, this.update);
    ListenEvent(Config.EventType.Fresh_Bag, this.update);
    ListenEvent(Config.EventType.RefreshMergePet,this.update)
    if itemCfg == nil then
        itemCfg = G.dataTable["t_item"];
    end

    return this;
end

-- 参数1： 道具itemId。nil代表 背包中的道具全部检测
function RedDotsBagItemHandler.getFlag(...)
    local flag = Config.RedDotsFlagState.None;
    local itemId = ...;
    local itemList = nil;
    if itemId ~= nil then
        itemList = {};
        local item = logicMgr.ItemManager.getItemData(itemId);
        if item ~= nil then
            table.insert(itemList, item);
        end
    else
        itemList = logicMgr.ItemManager.getItemDataByTabType(EnumConst.ItemTableEnum.Item);
    end
    for i = 1, #itemList do
        flag = this.getFlagByItem(itemList[i]);
        if flag == Config.RedDotsFlagState.Needful then
            break ;
        end
    end
    return flag;
end

function RedDotsBagItemHandler.getFlagByItem(itemData)
    local flag1 = Config.RedDotsFlagState.None;
    local tableData = itemCfg[tostring(itemData.itemModelId)];
    if tableData == nil or tableData.f_RedDot == 0 then
        return flag1;
    end
    local count = tonumber(itemData["num"]) or 0;
    if tonumber(itemData["scriptId"]) == EnumConst.EnuItem.RAND_AWARD then
        --随机型宝箱
        count = math.floor(count /tonumber(decodeJsonStrToTabel(itemData["itemFunction"])[4]))
    elseif tonumber(itemData["scriptId"]) == EnumConst.EnuItem.FIX_AWARD then
        --固定型宝箱
        count = math.floor(count /tonumber(decodeJsonStrToTabel(itemData["itemFunction"])[2]))
    elseif tonumber(itemData["scriptId"]) == EnumConst.EnuItem.PET_EGG then
        --飞飞蛋
        if not logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.Pet, false) then
            return flag1;
        end
        count = math.min(count, PetManager.GetPetBagSpace());
    elseif tableData.f_BagType == EnumConst.ItemTypeEnum.TimeBoxKey then
        count = 0
        if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.TimeBoxAll,false) then
            local flag,enough = logicMgr.ItemManager.IsTimeBoxKeyAndEnoughOpen(itemData.itemModelId)
            if flag then
                count = enough and 1 or 0
            end
        end
    end

    if count > 0 then
        flag1 = Config.RedDotsFlagState.Needful;
    end
    local flag2= Config.RedDotsFlagState.None;
    local constData = ItemManager.getItemConstDatabyModelId(itemData.itemModelId)
    if constData and constData.itemType == EnumConst.ItemTypeEnum.Hero then
        local hero=logicMgr.HeroManager.getHeroDatabyModelId(tonumber(itemData.itemModelId)%600000,true)
        flag2=RedDotsActivateHeroHandler.getSingleFlag(hero.partnerModelId)
        return flag2
    end
    return flag1
end

function RedDotsBagItemHandler.update()
    RedDotsManager.addBroadcast(Config.RedDotsType.BagItem);
end